package com.night.card.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.night.card.mapper.CardInMapper;
import com.night.card.service.interfs.CardInService;
import com.night.common.entity.card.Card;
import com.night.common.entity.card.CardIn;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: CardInServiceImp
 * @Description: 充值记录service实现
 * @author: CHX42
 * @date: 2022/10/21-9:28
 * @version: v1.0
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CardInServiceImp implements CardInService {
    @Value("${ss.card.pageSize}")
    private int pageSize;

    @Resource
    private CardInMapper cardInMapper;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public PageInfo<CardIn> getAll() {
        String pageName = "card_in_all";
        PageHelper.startPage(1, pageSize);
        List<CardIn> redisCardInList = (List<CardIn>) redisTemplate.opsForValue().get(pageName);
        if (redisCardInList != null)
            return new PageInfo<>(redisCardInList);
        PageHelper.startPage(1, pageSize);
        List<CardIn> cardInList = cardInMapper.select(new CardIn());
        redisTemplate.opsForValue().set(pageName, cardInList, 30, TimeUnit.SECONDS);
        return new PageInfo<>(cardInList);
    }

    @Override
    public PageInfo<CardIn> getBy(PageInfo<CardIn> pageInfo) {
        String pageName = "card_in_list_" + pageInfo.getPageNum() + "_" + pageInfo.getPageSize();
        PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
        List<CardIn> redisCardInList = (List<CardIn>) redisTemplate.opsForValue().get(pageName);
        if (redisCardInList != null)
            return new PageInfo<>(redisCardInList);
        PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
        List<CardIn> cardInList = cardInMapper.select(pageInfo.getList().get(0));
        redisTemplate.opsForValue().set(pageName, cardInList, 30, TimeUnit.SECONDS);
        return new PageInfo<>(cardInList);
    }

    @Override
    public PageInfo<CardIn> getByCardId(PageInfo<Card> pageInfo) {
        PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
        CardIn cardIn = new CardIn();
        cardIn.setCard(pageInfo.getList().get(0));
        List<CardIn> cardInList = cardInMapper.select(cardIn);
        return new PageInfo<>(cardInList);
    }
}
